﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace LightControls
{
    public enum LcAlertType
    {
        Success, Information, Error, Busy
    }
    
    public class LcAlertPanel: ContentControl
    {
        private Border rootBorder;
        private Grid rootGrid;
        private TextBlock AlertTextBlock;
        public LcAlertPanel()
        {
            this.DefaultStyleKey = typeof(LcAlertPanel);
            this.Alert = "";
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            rootBorder = (Border)this.GetTemplateChild("rootBorder");
            rootGrid = (Grid)this.GetTemplateChild("rootGrid");
            AlertTextBlock = (TextBlock)GetTemplateChild("AlertTextBlock");
            setup();
        }

        

        private void setup()
        {
            if (DesignerProperties.IsInDesignTool)
            {
                this.SetValue(AlertProperty, "i:Message Here");
            }
            // setup UI according to Alert
            string Alert = (string)this.GetValue(AlertProperty);
            if (string.IsNullOrEmpty(Alert))
            {
                this.Visibility = Visibility.Collapsed;
                return;
            }
            else
                this.Visibility = Visibility.Visible;

            int p = Alert.IndexOf(":");
            string alertType = "i";
            if (p > 0)
            {
                alertType = Alert.Substring(0, p).ToLower();
            }
            string alertMessage = Alert.Substring(p+1);
            if (AlertTextBlock != null)
                AlertTextBlock.Text = alertMessage;
            // setup UI according to Alert type
            //LcAlertType mt = (LcAlertType)this.GetValue(AlertTypeProperty);
            if (rootBorder != null)
            {
                if (alertType == "s" || alertType == "success")
                {
                    rootBorder.Background = (Brush)rootGrid.Resources["SucessBackgroundBrush"];
                    rootBorder.BorderThickness = new Thickness(1);
                    rootBorder.BorderBrush = (Brush)rootGrid.Resources["SucessStrokeBrush"];
                    AlertTextBlock.Foreground = (Brush)rootGrid.Resources["SucessStrokeBrush"];
                }
                else if (alertType == "b" || alertType == "busy")
                {
                    rootBorder.Background = (Brush)rootGrid.Resources["BusyBackgroundBrush"];
                    rootBorder.BorderThickness = new Thickness(1);
                    rootBorder.BorderBrush = (Brush)rootGrid.Resources["BusyStrokeBrush"];
                    AlertTextBlock.Foreground = (Brush)rootGrid.Resources["BusyStrokeBrush"];
                }
                else if (alertType == "e" || alertType == "error")
                {
                    rootBorder.Background = (Brush)rootGrid.Resources["ErrorBackgroundBrush"];
                    rootBorder.BorderThickness = new Thickness(1);
                    rootBorder.BorderBrush = (Brush)rootGrid.Resources["ErrorStrokeBrush"];
                    AlertTextBlock.Foreground = (Brush)rootGrid.Resources["ErrorStrokeBrush"];
                }
                else
                {
                    rootBorder.Background = (Brush)rootGrid.Resources["InfoBackgroundBrush"];
                    rootBorder.BorderThickness = new Thickness(1);
                    rootBorder.BorderBrush = (Brush)rootGrid.Resources["InfoStrokeBrush"];
                    AlertTextBlock.Foreground = (Brush)rootGrid.Resources["InfoStrokeBrush"];
                }
            }
        }

        private static void OnAlertChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            LcAlertPanel mp = (LcAlertPanel)d;
            mp.setup();
        }

        public static readonly DependencyProperty AlertProperty = DependencyProperty.Register(
            "Alert",
            typeof(string),
            typeof(LcAlertPanel),
            new PropertyMetadata(new PropertyChangedCallback(OnAlertChanged))
         );

       
        public string Alert
        {
            get
            {
                return this.GetValue(AlertProperty).ToString();
            }

            set
            {
                this.SetValue(AlertProperty, value);
            }
        }
    }
}
